package com.jy.wms.controller;

import com.jy.wms.common.PropertyUtil;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsCustomerFileWarehouse;
import com.jy.wms.service.WmsCustomerFileWarehouseService;
import com.jy.wms.service.WmsOwnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsCustomerFileWarehouseController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Liyi
 * @Date Fri Oct 13 09:52:56 CST 2017
 */
@RestController
@RequestMapping("/wmsCustomerFileWarehouse")
@Api(value = "/wmsCustomerFileWarehouse", description = "关于客户档案仓库详细信息管理操作")
public class  WmsCustomerFileWarehouseController extends BaseController<WmsCustomerFileWarehouseService,WmsCustomerFileWarehouse> {

    @Autowired
    private WmsOwnerService wmsOwnerService;

    @ApiOperation(value = "客户档案仓库管理 查询仓库及用户选中的ids",response = JSONObject.class, notes = "查询仓库及用户选中的ids 'sskhid'所属客户ID ")
    @PostMapping(value = "/list")
    public @ResponseBody JSONObject list( HttpSession session,@RequestBody String sskhid){
        JSONObject json = new JSONObject();
        try {
            String host = PropertyUtil.getProperty("safety.host");
            String port = PropertyUtil.getProperty("safety.port");
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzmc = "";
            if(hz.size()>0){
                hzmc = hz.get(0).getShow_hzid();
            }
            List<Integer> list = wmsOwnerService.getAllByMc(hzmc);
            List<String> ids = baseService.getAllBySskhid(sskhid);
            String id = JSONArray.fromObject(list).toString();
            String result = HttpUtil.doPost("http://"+host+":"+port+"/ms-safety/org/getorgbyids" ,id);
            JSONArray jsonReturn = new JSONArray();
            json.put("ids",ids);
            json.put("rows",JSONArray.fromObject(result));
        }catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "客户档案仓库管理 新增客户档案仓库信息",response = JSONObject.class, notes = "新增客户档案仓库信息")
    public @ResponseBody JSONObject save(HttpSession session, @RequestBody SetAndModifyModel setAndModifyModel){
        try {
            baseService.baseInsert(setAndModifyModel);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增客户档案仓库信息成功","", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"新增客户档案仓库信息失败",e.getMessage(),"");
        }catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增客户档案仓库信息成功","", "");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"新增客户档案仓库信息失败",e.getMessage(),"");
        }
    }

    @PostMapping(value = "/edit/{sshzid}")
    @ApiOperation(value = "客户档案仓库管理 修改客户档案仓库信息",response = JSONObject.class, notes = "修改客户档案仓库信息")
    public @ResponseBody JSONObject edit(HttpSession session, @RequestBody SetAndModifyModel setAndModifyModel, @PathVariable String sshzid){
        try {
            baseService.editBySskhid(sshzid,setAndModifyModel);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改客户档案仓库信息成功","", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改客户档案仓库信息失败",e.getMessage(),"");
        } catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改客户档案仓库信息成功","", "");
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改客户档案仓库信息失败",e.getMessage(),"");
        }
    }

    @PostMapping(value = "/getLineByDz")
    @ApiOperation(value = "客户档案仓库管理 修改客户档案仓库信息",response = JSONObject.class, notes = "修改客户档案仓库信息")
    public @ResponseBody JSONArray getLineByDz(HttpSession session, @RequestBody WmsCustomerFileWarehouse wmsCustomerFileWarehouse){
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        try {
            String hzid = "0";
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                wmsCustomerFileWarehouse.setHzid(hzid = hz.get(0).getHzid());
            }else{
                wmsCustomerFileWarehouse.setHzid("0");
            }
            list = baseService.getLineByDz(wmsCustomerFileWarehouse);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(list);
    }
}
